package com.be.beadmin.project.controller;

import com.be.beadmin.basicData.dto.input.UserDto;
import com.be.beadmin.basicData.entity.User;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.InfoAndPayDto;
import com.be.beadmin.project.dto.input.InfoDto;
import com.be.beadmin.project.dto.input.ProjectNameAndCode;
import com.be.beadmin.project.dto.output.UserInfoSimpEx;
import com.be.beadmin.project.entity.Info;
import com.be.beadmin.project.mapper.InfoMapper;
import com.be.beadmin.project.repository.InfoRepository;
import com.be.beadmin.project.repository.ProductInfoRepository;
import com.be.beadmin.project.repository.ProductTotalInfoRepository;
import com.be.beadmin.project.service.BatchPlanService;
import com.be.beadmin.project.service.InfoService;
import com.be.beadmin.project.service.MasterPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;

/**
 * @author zmy
 * @date 2023-01-13
 **/
@Api(tags = {"项目管理-项目管理"})
@RestController

public class

InfoController {

    @Autowired
    private InfoService infoService;
//    @Autowired
//    private InfoService infoService;
    @Autowired
    private InfoRepository infoRepository;
    @Autowired
    private InfoMapper infoMapper;
    @Autowired
    private ProductTotalInfoRepository productTotalInfoRepository;
    @Autowired
    private MasterPlanService masterPlanService;
    @Autowired
    private BatchPlanService batchPlanService;
    @Autowired
    private ProductInfoRepository productInfoRepository;

    // 查询
    @ApiOperation("获取项目分页信息")
    @PostMapping(value = "/project/getInfoByPage")
    public ResultModel getInfoByPage(@RequestBody PageEntity pageEntity) {
        return ResultModel.success(infoService.getInfoByPage(pageEntity));
    }

    @ApiOperation("获取指定用户的所有项目信息")
    @PostMapping(value = "/project/getInfoByUser")
    public ResultModel getInfoByUser(@RequestBody User user) {
        return ResultModel.success(infoService.getInfoByUser(user));
    }

    @ApiOperation("获取当前用户的所有项目信息")
    @PostMapping(value = "/project/getInfoByCurrentUser")
    public ResultModel getInfoByCurrentUser(HttpServletRequest request) {
        return ResultModel.success(infoService.getInfoByCurrentUser(request));
    }

    @ApiOperation("根据项目编码获取项目信息")
    @PostMapping(value = "/project/getInfoByProjectCode")
    public ResultModel getInfoByProjectCode(@RequestParam String projectCode) {
        return ResultModel.success(infoService.getInfoByProjectCode(projectCode));
    }


    @ApiOperation("获取当前用户的所有项目Id")
    @PostMapping(value = "/project/getInfoIdByCurrentUser")
    public ResultModel getInfoIdByCurrentUser(HttpServletRequest request) {
        return ResultModel.success(infoService.getInfoIdByCurrentUser(request));
    }
//    @ApiOperation("获取项目及付款分页信息")
//    @PostMapping(value = "/project/getInfoAndPayByPage")
//    public ResultModel getInfoAndPayByPage(@RequestBody PageEntity pageEntity) {
//        return ResultModel.success(infoService.getInfoAndPayByPage(pageEntity));
//    }



//    @ApiOperation("获取项目及付款分页信息")
//    @PostMapping(value = "/project/getInfoAndPayByPage")
//    public ResultModel getInfoAndPayByPage(@RequestBody PageEntity pageEntity) {
//        return ResultModel.success(infoService.getInfoAndPayByPage(pageEntity));
//    }

//    @ApiOperation("获取项目及付款分页信息")
//    @PostMapping(value = "/project/getInfoAndPayByPage")
//    public ResultModel getInfoAndPayByPage(@RequestBody PageEntity pageEntity) {
//        return ResultModel.success(infoService.getInfoAndPayByPage(pageEntity));
//    }

    @ApiOperation("获取所有项目的项目名称和编码")
    @PostMapping("/project/getProjectNameAndCodeSimp")
    public ResultModel getProjectNameAndCodeSimp(){
        List<ProjectNameAndCode> projectInfoSimps = infoService.getProjectNameAndCodeSimp();
        return ResultModel.success(projectInfoSimps);
    }


    @ApiOperation("暂停/恢复执行项目")
    @PostMapping(value = "/project/pauseOrStartProject")
    public ResultModel pauseOrStartProject(@RequestParam String infoId) {
        return ResultModel.success(infoService.pauseOrStartProject(infoId));
    }
    @ApiOperation("关闭/开启执行项目")
    @PostMapping(value = "/project/shutOrStartProject")
    public ResultModel shutOrStartProject(@RequestParam String infoId) {
        return ResultModel.success(infoService.shutOrStartProject(infoId));
    }

    // 新增
    @ApiOperation("添加一个项目")
    @PostMapping("/project/addInfo")
    public ResultModel addInfo(@RequestBody InfoDto infoDto) {

//        return ResultModel.success(infoService.addInfo(infoDto));

        int res = infoService.addInfo(infoDto);
        if (res == -1) {
            System.out.println("yeah:" + res);
            return ResultModel.error(-1, "该项目已有主计划，请勿重复添加");
        }
        return ResultModel.success("项目添加成功");
    }

    @ApiOperation("添加一个项目")
    @PostMapping("/project/addInfoAndPay")
    public ResultModel addInfoAndPay(@RequestBody InfoAndPayDto infoAndPayDto) {

//        return ResultModel.success(infoService.addInfoAndPay(infoAndPayDto));

        int res = infoService.addInfoAndPay(infoAndPayDto);
        if (res == -1) {
            System.out.println("yeah:" + res);
            return ResultModel.error(-1, "该项目已有主计划，请勿重复添加");
        }
        return ResultModel.success("项目添加成功");
    }

    @ApiOperation("返回所有国家列表")
    @PostMapping("/project/getListCountry")
    public List<String> getListCountry() {

        String[] locales = Locale.getISOCountries();
        List<String> strings = new ArrayList<>();
        for (String countryCode : locales) {
            Locale obj = new Locale("", countryCode);
            strings.add(obj.getDisplayCountry(Locale.CHINESE));


        }
        return strings;
    }

    // 删除
//    @ApiOperation("删除一个项目")
//    @PostMapping("/project/delInfo")
//    public ResultModel delInfo(HttpServletRequest request,@RequestBody InfoDto infoDto) {
//        infoService.delInfo(request,infoDto.getId());
//        return ResultModel.success(ResultModel.DELETE);
//    }
    // 修改
    @ApiOperation("修改一个项目")
    @PostMapping("/project/updateInfo")
    public ResultModel updateInfo(@RequestBody InfoDto infoDto) {
        infoService.updateInfo(infoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("获取合同编码和项目编码")
    @PostMapping("/project/getContractAndInfoCodeNext")
    public ResultModel getContractAndInfoCodeNext() {
        return ResultModel.success(infoService.getContractAndInfoCodeNext());
    }

    @ApiOperation("项目审核")
    @PostMapping("/project/infoReview")
    public ResultModel infoReview(@ApiParam(value = "项目id",required = true) @RequestParam String InfoId,
                                    //@ApiParam(value = "用户id",required = true) @RequestParam String userId,
//                                    @ApiParam(value = "审核状态",required = true) @RequestParam(required = false) int reviewState,
                                    @ApiParam(value = "审核备注") @RequestParam String reviewMemo,
                                    @ApiParam(value = "附件") @RequestParam("file") MultipartFile multipartFile
        ) throws Exception {

        infoService.infoReview(InfoId,reviewMemo,multipartFile);
        return ResultModel.success("审核成功！");
    }
    @ApiOperation("根据项目ID获取该项目的所有用户列表")
    @PostMapping("/project/getUsersByInfoId")
    public ResultModel getUsersByInfoId(@RequestParam String infoId){
        List<UserInfoSimpEx> userLists = infoService.getUsersByInfoId(infoId);
        return ResultModel.success(userLists);
    }

    @ApiOperation("获取项目进度")
    @PostMapping("/project/getInfoProgress")
    public ResultModel getInfoProgress(){
        ArrayList<Integer> infoProgress = new ArrayList<>();
        infoProgress = infoService.getInfoProgress();
        return ResultModel.success(infoProgress);
    }

    @ApiOperation("获取项目名称列表")
    @PostMapping("/project/getProjectNameList")
    public ResultModel getProjectNameList(){
        return ResultModel.success(infoService.getProjectNameList());
    }

    @ApiOperation("根据项目名称获取项目编号")
    @PostMapping("/project/getProjectCodeByProjectName")
    public ResultModel getProjectCodeByProjectName(@RequestParam String projectName){
        return ResultModel.success("success" , infoService.getProjectCodeByProjectName(projectName));
    }

}